# GNU Guix --- Functional package management for GNU
# Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
# Copyright © 2021 Maxime Devos <maximedevos@@gnu.org>
#
# This file is part of GNU Guix.
#
# GNU Guix is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# GNU Guix is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

# based upon test-env

# run this in a
# guix environment guix --ad-hoc gnunet

# Start GNUnet
gnunetdir=`guix build gnunet`
OLDPATH=$PATH
PATH=$gnunetdir/bin:$gnunetdir/lib/gnunet/libexec:$PATH
export PATH; hash

function on_exit ()
{
    if [ -n "$gfs_pid" ]
    then
	kill "$gfs_pid"
    fi
    if [ -n "$gds_pid" ]
    then
	kill "$gds_pid"
    fi
    if [ -n "$daemon_pid" ]
    then
	kill "$daemon_pid"
    fi
    if [ -n "$gpublish_pid" ]
    then
	kill "$gpublish_pid"
    fi
}

trap on_exit EXIT

gnunet-service-fs&
gfs_pid=$!
gnunet-service-datastore&
gds_pid=$!

####

TESTDIR=$PWD/gtest
NIX_STORE_DIR=$TESTDIR/store
GUIX_LOG_DIRECTORY=$TESTDIR/log
GUIX_DATABASE_DIRECTORY=$TESTDIR/db
GUIX_STATE_DIRECTORY=$TESTDIR/state
GUIX_CONFIGURATION_DIRECTORY=$TESTDIR/CONFIGURATION

# The configuration directory, for import/export signing keys.
GUIX_CONFIGURATION_DIRECTORY="/home/sylviidae/guix/git/guix-download-hook/test-tmp/etc"
mkdir -p $GUIX_LOG_DIRECTORY $GUIX_DATABASE_DIRECTORY $GUIX_STATE_DIRECTORY $GUIX_CONFIGURATION_DIRECTORY $NIX_STORE_DIR
if [ ! -d "$GUIX_CONFIGURATION_DIRECTORY" ]
then
	# Copy the keys so that the secret key has the right permissions (the
	# daemon errors out when this is not the case.)
	mkdir -p "$GUIX_CONFIGURATION_DIRECTORY"
	cp "$PWD/tests/signing-key.sec"	\
	    "$PWD/tests/signing-key.pub"	\
	    "$GUIX_CONFIGURATION_DIRECTORY"
	chmod 400 "$GUIX_CONFIGURATION_DIRECTORY/signing-key.sec"
fi

GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES=yes
XDG_CACHE_HOME="$GUIX_STATE_DIRECTORY/cache"
mkdir -p "$XDG_CACHE_HOME"

GUIX_BINARY_SUBSTITUTE_URL="http://localhost:7777"

export NIX_IGNORE_SYMLINK_STORE NIX_STORE_DIR		\
       GUIX_LOG_DIRECTORY GUIX_STATE_DIRECTORY GUIX_DATABASE_DIRECTORY	\
       GUIX_BINARY_SUBSTITUTE_URL				\
       GUIX_ALLOW_UNAUTHENTICATED_SUBSTITUTES			\
       GUIX_CONFIGURATION_DIRECTORY XDG_CACHE_HOME

# Launch the daemon without chroot support because is may be
# unavailable, for instance if we're not running as root.
# Also enable the GNUnet substituter
"$PWD/pre-inst-env" "$PWD/guix-daemon"				\
        --substitute-methods="gnunet http" \
	--disable-chroot	\
	--substitute-urls="$GUIX_BINARY_SUBSTITUTE_URL" &

daemon_pid=$!

# Avoid issues that could stem from l10n, such as language/encoding
# mismatches.
unset LANGUAGE
LC_MESSAGES=C
export LC_MESSAGES

# Disable grafts by default because they can cause things to be built
# regardless of '--dry-run'.
GUIX_BUILD_OPTIONS="--no-grafts"
export GUIX_BUILD_OPTIONS

# Ignore user settings.
unset GUIX_PACKAGE_PATH

# Make sure the daemon is running
./pre-inst-env guile -c \
	     '(use-modules (guix))
(let loop ((i 10))
  (catch #t
    (lambda () (open-connection))
    (lambda (key . args)
      (if (zero? i)
          (apply throw key args)
          (begin (usleep 500000) (loop (- i 1)))))))'

# The test suite expects the 'guile-bootstrap' package to be available.
# Normally the Guile bootstrap tarball is downloaded by a fixed-output
# derivation but when network access is missing we allow users to drop
# the tarball in 'gnu/packages/bootstrap/SYSTEM' and "intern" it here.
bootstrap_directory1="$PWD/gnu/packages/bootstrap/x86_64-linux"
bootstrap_directory2="$PWD/gnu/packages/bootstrap/i686-linux"
if [ -d "$bootstrap_directory1" ]
then
   for file in "$bootstrap_directory1"/guile-*
   do
       [ -f "$file" ] &&					\
	   "./pre-inst-env"			\
	       guix download "file://$file" > /dev/null
   done
fi
if [ -d "$bootstrap_directory2" ]
then
   for file in "$bootstrap_directory2"/guile-*
   do
       [ -f "$file" ] &&					\
	   "./pre-inst-env"			\
	       guix download "file://$file" > /dev/null
   done
fi

# Build something to substitute
./pre-inst-env guix build -e '(@@ (gnu packages commencement) bootar)' --no-substitutes
store_path=`./pre-inst-env guix build -e '(@@ (gnu packages commencement) bootar)'`
hash=`echo $store_path | sed -E -e 's/^(.*)\/(.*)-bootar-1a/\2/'`

# Make sure the substitute is cached & publish it
mkdir "$TESTDIR/cache-dir"
./pre-inst-env guix publish --port=7777 --listen=localhost --cache="$TESTDIR/cache-dir" \
	       --hooks=$PWD/hook.scm --public-key=tests/signing-key.pub \
	       --secret-key=tests/signing-key.sec&
gpublish_pid=$!
sleep 3

./pre-inst-env guix download http://localhost:7777/$hash.narinfo

# ^ this should have a GNUnet: gnunet://fs/chk/... line

# Let's try substitution now, in an environment without a network!

echo "Please disconnect from the network"
echo "(Not strictly necessary)"
echo "(Only to demonstrate the substitution happens via GNUnet)"
./pre-inst-env guix gc --delete "$store_path"
sleep 4
echo "It's assumed you have been disconnected!"
./pre-inst-env guix build -e '(@@ (gnu packages commencement) bootar)'

echo "The above substitution should have happened via GNUnet!"
